$(function(){
	var userGrid = $("#usersGrid");
	  var userGridAction = $("#userGridAction");
	//初始化
	userGrid.datagrid({
		url:'emp/listusers',
		fit:true,
		fitColumns:true,
		rownumbers:true,
		pagination : true,// 是否显示分页栏
		columns : [ [ {
			field : 'account',
			title : '账号',
			align : 'center',
			width : 100
		},  {
			field : 'name',
			title : '姓名',
			align : 'center',
			width : 100
		},{
			field : 'emp',
			title : '性别',
			width : 100,
			align : 'center',
			formatter: function (value, rec) {
				if(value!=null){
					if (rec.emp.sex == "1") {
						return "<img src='public/images/male.png'>";
					}else{
						return "<img src='public/images/female.png'>";
					}
				}else{
					if (rec.student.gender == "男") {
						return "<img src='public/images/male.png'>";
					}else{
						return "<img src='public/images/female.png'>";
					}
				}
				
		     }
		}, {
			field : 'student',
			title : '角色',
			width : 100,
			align : 'center',
			formatter: function (value, rec) {
				if(value!=null){
					return "<div style='color:#fff;background-color:#66CC66;border-radius:5px;width:40px;height:20px;line-height:20px;margin:0px auto'>学生</div>";
				}else{
					return rec.emp.job;
				}
				/*var roles = [];
		          $.each(val, function () {
		            roles.push(this.roleName);
		          });
		          return roles.join(",");*/
		     }
		},
	      {
	        field: 'enable', title: '状态', width: 80, align: 'center', formatter: function (val) {
	          return val ? "<div style='color:#fff;background-color:#66CC66;border-radius:10px;width:40px;height:20px;line-height:20px;margin:0px auto'>可用</div>" : "<div style='color:#fff;background-color:red;border-radius:10px;width:40px;height:20px;line-height:20px;margin:0px auto'>禁用</div>";
	        }
	      },
			{
				field : 'ac',
				title : '操作',
				width : 120,
				align : 'center',
				formatter:function(value,row,index){
					var bts = [];
					bts.push('<a data-index="'+index+'" class="ctr edit">编辑</a>');
					bts.push('<a data-id="'+row.id+'" data-index="'+index+'" class="ctr delete">删除</a>');
					return bts.join('');
				}
			}
	      ] ],
	      toolbar: "#userGridToolbar"
	});
	
	
	var account = $("#account");
	
	//检索按钮时间
	$("#usersSearch").on("click",function(){
		userGrid.datagrid("load",{
			account:"%"+account.val()+"%"
		});
	});
	var form = $("#usersSearchForm");

	$("#usersReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});
	
	
	var gridPanel = userGrid.datagrid("getPanel");

	  //给操作按钮绑定事件
	  gridPanel.on("click", "a.edit", function () {
	    var id = this.dataset.id;
	    formDialog(id);
	  }).on("click", "a.delete", function () {
	    var id = this.dataset.id;
	    $.messager.confirm("提示", "是否删除？", function (r) {
	      if (r) {
	        $.get("/user/delete?id=" + id).success(function () {
	          //删除成功
	          userGrid.datagrid("reload");
	        });
	      }
	    })
	  }).on("click", "a.create", function () {
	    formDialog();
	  });
	  
	  /**
	   * 表单窗口
	   */
	  function formDialog(id) {
	    var dialog = $("<div/>").dialog({
	      iconCls: 'icon-edit',
	      title: (id ? '编辑' : '创建') + '用户',
	      href: '/user/' + (id ? 'load?id=' + id : 'form'),
	      width: 380,
	      height: 360,
	      modal: true,
	      onClose: function () {
	        //销毁窗口
	        $(this).dialog("destroy");
	      },
	      buttons: [
	        {
	          text: '保存',
	          handler: function () {
	            var userForm = $("#userForm");
	            if (userForm.form("validate")) {
	              $.post("/user/" + (id ? 'update' : 'save'),
	                userForm.serialize()
	              ).success(function () {
	                userGrid.datagrid("reload");
	                dialog.dialog('close');
	              });
	            }
	          }
	        }
	      ]
	    });
	  }
});